
#include "GLView.h"

GLView::GLView(){

    // Utilisé pour récupérer les événéments dans la fenêtre
    setFocusPolicy(Qt::StrongFocus);

    makeCurrent();
    glewInit();
    pressed=false;


    img = new Image();

    img->Load("../Resources/original.tga");
    img->GenTexture();
    img->Debug();


    context =   new GLContext();
    context->LoadProgram("basic", "../Resources/basique.frag", "../Resources/basique_2D.vert");


    viewEntity    = new View(PERSPECTIVE);

    UniformMat4 *   view    =   new UniformMat4(viewEntity->m_View);
    UniformMat4 *   proj    =   new UniformMat4(viewEntity->m_Projection);
    UniformMat4 *   model    =   new UniformMat4();

    view->m_Name    =   "ViewMatrix";
    proj->m_Name    =   "ProjectionMatrix";
    model->m_Name   =   "ModelMatrix";

    VertexTemplate  * vertexTemplate = new VertexTemplate();

    vertexTemplate->AddAttribute(4, "position");
    vertexTemplate->AddAttribute(4,"color");
    vertexTemplate->AddAttribute(4,"normal");
    vertexTemplate->AddAttribute(4,"texCoord1");


    Material * material     = new Material();
    material->SetVertexTemplate(*vertexTemplate);

    material->Program(*context->program);
    material->AddNewSharedUniform(view);
    material->AddNewSharedUniform(proj);

    material->AddNewUniform(model);

    material->GetLocations();


    mesh = new Mesh(vertexTemplate);

    Vertex v1 = vertexTemplate->NewVertex();
    v1.SetAttribute("position", -1.0, -1.0, -0.0, 1.0);
    v1.SetAttribute("color", 1.0, 0.0, 0.0, 1.0);
    v1.SetAttribute("normal", 1.0, 0.0, 0.0, 1.0);
    v1.SetAttribute("texCoord1",0.0, 1.0, 0.0, 0.0);

    Vertex v2 = vertexTemplate->NewVertex();
    v2.SetAttribute("position", 1.0, -1.0, -0.0, 1.0);
    v2.SetAttribute("color", 0.0, 1.0, 0.0, 1.0);
    v2.SetAttribute("normal", 0.0, 0.0, 0.0, 1.0);
    v2.SetAttribute("texCoord1",1.0, 1.0, 1.0, 0.0);

    Vertex v3 = vertexTemplate->NewVertex();
    v3.SetAttribute("position", 1.0, 1.0, -0.0, 1.0);
    v3.SetAttribute("color", 0.0, 0.0, 1.0, 1.0);
    v3.SetAttribute("normal", 0.0, 0.0, 0.0, 1.0);
    v3.SetAttribute("texCoord1",1.0, 0.0, 1.0, 1.0);

    Vertex v4 = vertexTemplate->NewVertex();
    v4.SetAttribute("position", -1.0, 1.0, -0.0, 1.0);
    v4.SetAttribute("color", 0.0, 0.0, 1.0, 1.0);
    v4.SetAttribute("normal", 0.0, 0.0, 0.0, 1.0);
    v4.SetAttribute("texCoord1",0.0, 0.0, 0.0, 1.0);


    Vertex::Debug();
    mesh->NewTriangle(v3,v2,v1);
    mesh->NewTriangle(v1,v3,v4);
    Triangle::Print();

    enti2    =   new Entity();

    enti2->m_Mesh=mesh;
    enti2->m_Material=material;


    enti    =   new Entity();

    enti->m_Mesh=mesh;
    enti->m_Material=   material;

    material->AttachTo(enti);
    material->AttachTo(enti2);

    enti->m_Transform   =   new Transform( ((UniformMat4*)(enti->m_Uniforms.front()))->m_Matrix);
    enti2->m_Transform   =   new Transform( ((UniformMat4*)(enti2->m_Uniforms.front()))->m_Matrix);


    enti->m_Transform->Translation(2.0f,0.0f,-10.0f);
    enti2->m_Transform->Translation(-2.0f,0.0f,-10.0f);


    enti->m_image=img;
    enti2->m_image=img;

    BufferObject::FeedBuffers();

}

void GLView::mouseMoveEvent(QMouseEvent *event){
    if(pressed==false){
        pressed=true;
        anchorX= event->x();
        anchorY=event->y();
    }else{
        float movx    =   anchorX- event->x();
        float movy    =   anchorY-event->y();
        float coef=0.01f;

       // qDebug()<<movx;

        movx    =   movx* coef;
        movy    =   movy*coef;

        anchorX= event->x();
        anchorY=event->y();

        viewEntity->transform->RotateEuler(-movy, -movx,0.0f);
        updateGL();
    }
}

void GLView::mouseReleaseEvent(QMouseEvent *event){
    pressed=false;
}

void GLView::keyPressEvent(QKeyEvent *key){
    if(key->key()== Qt::Key_Z){
        vec4 dir =viewEntity->transform->Forward();
         if(dir.z>0){
             dir.y= -dir.y;
         }
        dir.z= -dir.z;


        viewEntity->transform->Translate(  dir*0.1f);
        viewEntity->m_View->Print();

    }
     if(key->key()== Qt::Key_S){
        vec4 dir =viewEntity->transform->Forward();
        if(dir.z>0){
            dir.y= -dir.y;
        }
         dir.z= -dir.z;
        viewEntity->transform->Translate(  dir*-0.1f);
        viewEntity->m_View->Print();
    }

    if(key->key()== Qt::Key_Q){
        vec4 dir =viewEntity->transform->Right();
        if(dir.z>0){
            dir.y= -dir.y;
        }
         dir.z= -dir.z;
        viewEntity->transform->Translate(  dir*0.1f);
        viewEntity->m_View->Print();

    }
     if(key->key()== Qt::Key_D){
        vec4 dir =viewEntity->transform->Right();
        if(dir.z>0){
            dir.y= -dir.y;
        }
         dir.z= -dir.z;
        viewEntity->transform->Translate(  dir*-0.1f);
        viewEntity->m_View->Print();
    }

    updateGL();
}

void GLView::initializeGL(){

    glClearColor(0.7, 0.7, 0.7, 0.0);
    glEnable(GL_DEPTH_TEST);
}

void GLView::paintGL(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    BufferObject::Bind();
    //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    enti->Display();
    enti2->Display();
}

void GLView::resizeGL(int w, int h){
    glViewport(0, 0, w, h);
}
